package com.example.administrator.rxjavarxandroid_retrofit.reject.moudle;

import android.app.Application;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;

import com.example.administrator.rxjavarxandroid_retrofit.App;
import com.example.administrator.rxjavarxandroid_retrofit.R;
import com.example.administrator.rxjavarxandroid_retrofit.common.Constants;
import com.example.administrator.rxjavarxandroid_retrofit.http.api.CommonApi;
import com.example.administrator.rxjavarxandroid_retrofit.http.factory.ApiErrorAwareConverterFactory;
import com.example.administrator.rxjavarxandroid_retrofit.http.factory.EmptyJsonLenientConverterFactory;
import com.example.administrator.rxjavarxandroid_retrofit.http.factory.HttpsFactroy;
import com.example.administrator.rxjavarxandroid_retrofit.http.filter.AuthInterceptor;
import com.example.administrator.rxjavarxandroid_retrofit.http.filter.ResponseInterceptor;
import com.example.administrator.rxjavarxandroid_retrofit.reject.ContextLife;

import java.util.concurrent.TimeUnit;

import javax.inject.Singleton;

import dagger.Module;
import dagger.Provides;
import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by chenboge on 2016/12/18.
 */
@Module()
public class AppModule {

    App mApplication;

    public AppModule(App application) {
        mApplication = application;
    }

    @Provides
    @Singleton
    @ContextLife("Application")
    public App provideApp() {
        return mApplication;
    }

    // Dagger will only look for methods annotated with @Provides
    @Provides
    @Singleton
    SharedPreferences providesSharedPreferences(Application application) {
        return PreferenceManager.getDefaultSharedPreferences(application);
    }

    //新版本interceptor
    @Provides
    @Singleton
    OkHttpClient provideOkHttpClient() {
        HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
        loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);

        OkHttpClient okHttpClient = new OkHttpClient.Builder()
                .readTimeout(Constants.HTTP_CONNECT_TIMEOUT, TimeUnit.MILLISECONDS)
                .connectTimeout(Constants.HTTP_CONNECT_TIMEOUT, TimeUnit.MILLISECONDS)
                .socketFactory(HttpsFactroy.getSSLSocketFactory(mApplication, Constants.certificates))//certificates 是你raw下证书源ID, int[] certificates = {R.raw.myssl}
                .hostnameVerifier(HttpsFactroy.getHostnameVerifier(Constants.hosts))//hosts是你的host数据 列如 String hosts[]`= {“https//:aaaa,com”, “https//:bbb.com”}
                .addInterceptor(loggingInterceptor)
                .addInterceptor(new AuthInterceptor())
                .addInterceptor(new ResponseInterceptor())
                .build();

        return okHttpClient;
    }


    //新版本
    @Provides
    @Singleton
    CommonApi provideHotApi(OkHttpClient okHttpClient) {
        Retrofit retrofit1 = new Retrofit.Builder()
                .baseUrl(Constants.Base_Url)
                .client(provideOkHttpClient())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .addConverterFactory(new ApiErrorAwareConverterFactory(new EmptyJsonLenientConverterFactory(GsonConverterFactory.create())))
                .build();
        return retrofit1.create(CommonApi.class);
    }


}
